using System;
using System.Text;

namespace Util.Helper
{
    public class RandomHelper
    {
        /// <summary>
        /// 随机位数的数字
        /// </summary>
        /// <param name="digit">位数</param>
        /// <param name="feed">种子</param>
        /// <returns></returns>
        public static string RandomNum(int digit, int? feed = null)
        {
            Random rnd;
            if (feed != null)
            {
                rnd = new Random(feed.Value);
            }
            else
            {
                rnd = new Random();
            }
            string[] nums = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < digit; i++)
            {
                sb.Append(nums[rnd.Next(0, 9)].ToString());
            }
            return sb.ToString();
        }

        /// <summary>
        /// 随机位数的字符
        /// </summary>
        /// <param name="digit">位数</param>
        /// <param name="feed">种子</param>
        /// <returns></returns>
        public static string RandomCode(int digit, int? feed = null)
        {
            Random rnd;
            if (feed != null)
            {
                rnd = new Random(feed.Value);
            }
            else
            {
                rnd = new Random();
            }
            char[] code = "0123456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ".ToCharArray();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < digit; i++)
            {
                sb.Append(code[rnd.Next(0, code.Length - 1)].ToString());
            }
            return sb.ToString();
        }
    }
}
